﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HCFramework.API.LINQ;

namespace C
{
    public class PropertyFilterController
    {
        public static void Insert(PropertyFilter a)
        {
            // Khong insert neu trung ten
            if (GetByName(a.Value, a.ProductPropertyID) != null) return;

            HCDataContext db = new HCDataContext();
            db.PropertyFilters.InsertOnSubmit(a);
            db.SubmitChanges();
        }

        public static void Update(PropertyFilter a, HCDataContext db)
        {
            db.SubmitChanges();
        }

        public static void Delete(int id)
        {
            HCDataContext db = new HCDataContext();
            PropertyFilter a = db.PropertyFilters.Where(
                ik => ik.PropertyFilterID == id).First();

            // Xoa record trong database
            db.PropertyFilters.DeleteOnSubmit(a);
            db.SubmitChanges();
        }

        public static PropertyFilter GetByID(int id, HCDataContext db)
        {
            return db.PropertyFilters.Where(
                ik => ik.PropertyFilterID == id).FirstOrDefault();
        }

        public static PropertyFilter GetByID(int id)
        {
            return GetByID(id, new HCDataContext());
        }

        public static PropertyFilter GetByName(string name, int propertyID)
        {
            return new HCDataContext().PropertyFilters.Where(ik =>
                ik.ProductPropertyID == propertyID
                && ik.Value == name).FirstOrDefault();
        }
    }
}